<?php

namespace common\models;

use Yii;

/**
 * This is the model class for table "member".
 *
 * @property integer $member_id
 * @property string $mobile
 * @property string $member_name
 * @property string $nickname
 * @property string $email
 * @property string $password
 * @property string $pay_password
 * @property string $password_reset_token
 * @property string $auth_key
 * @property string $token
 * @property string $avatar
 * @property string $truename
 * @property string $identity
 * @property integer $sex
 * @property string $birthday
 * @property integer $email_subscriptions
 * @property integer $inviter_id
 * @property string $invitation_code
 * @property integer $country_id
 * @property integer $province_id
 * @property integer $city_id
 * @property integer $area_id
 * @property string $pca_info
 * @property string $address
 * @property integer $status
 * @property integer $is_del
 * @property integer $register_time
 * @property integer $first_login_time
 * @property integer $login_time
 * @property integer $old_login_time
 * @property integer $del_time
 * @property string $login_ip
 * @property string $old_login_ip
 * @property string $introduction
 * @property integer $role
 * @property integer $parent_role
 * @property integer $ancestors_role
 * @property integer $under_review
 * @property integer $position
 * @property integer $office
 * @property MemberAddress[] $memberAddresses
 */
class Member extends \yii\db\ActiveRecord
{

    //用户状态
    const STATUS_ENABLE = 1;
    const STATUS_DISABLE = 0;
    //是否删除
    const DEL_TRUE = 1;
    const DEL_FALSE = 0;
    //场景
    //用户可自身修改的信息
    const SCENARIO_SELF_UPDATE = 'self_update';

    private $_Member;

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'member';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['mobile', 'password', 'invitation_code', 'register_time'], 'required'],
            [['sex', 'email_subscriptions', 'inviter_id', 'country_id', 'province_id', 'city_id', 'area_id', 'status',
                'is_del', 'register_time', 'first_login_time', 'login_time', 'old_login_time', 'del_time','role','parent_role','ancestors_role'], 'integer'],
            [['birthday'], 'safe'],
            [['mobile'], 'string', 'max' => 20],
            [['member_name', 'nickname', 'email', 'truename'], 'string', 'max' => 50],
            [['password', 'pay_password', 'password_reset_token', 'auth_key', 'token', 'avatar'], 'string', 'max' => 100],
            [['identity'], 'string', 'max' => 18],
            [['invitation_code'], 'string', 'max' => 8],
            [['pca_info', 'address', 'introduction', 'position', 'office'], 'string', 'max' => 255],
            [['login_ip', 'old_login_ip'], 'string', 'max' => 15],
            [['mobile'], 'unique', 'message' => '当前号码已被注册'],
            [['invitation_code'], 'unique', 'message' => '注册失败，请重试'],

            ['sex', 'default', 'value' => 0],
            ['email_subscriptions', 'default', 'value' => 0],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'member_id' => '会员ID',
            'mobile' => '手机号码',
            'member_name' => '会员名称（账号）',
            'nickname' => '昵称',
            'email' => '邮箱',
            'password' => '会员密码',
            'pay_password' => '支付密码',
            'password_reset_token' => '重置密码token',
            'auth_key' => '自动登录key',
            'token' => '登录token',
            'avatar' => '会员头像',
            'truename' => '真实姓名',
            'identity' => '用户身份证号码',
            'sex' => '会员性别',
            'birthday' => '生日',
            'email_subscriptions' => '邮件订阅',
            'inviter_id' => '邀请人ID',
            'invitation_code' => '唯一邀请码',
            'country_id' => '国家',
            'province_id' => '省',
            'city_id' => '市',
            'area_id' => '区',
            'pca_info' => '省市区',
            'address' => '详细地址',
            'status' => '状态',
            'is_del' => '是否删除',
            'register_time' => '注册时间',
            'first_login_time' => '第一次登陆时间',
            'login_time' => '当前登录时间',
            'old_login_time' => '上次登录时间',
            'del_time' => '删除时间',
            'login_ip' => '当前登录ip',
            'old_login_ip' => '上次登录ip',
            'introduction' => '简介',
            'role' => '所属部门',
            'parent_role' => '父级id',
            'ancestors_role' => '祖级id',
            'verify_code' => '验证码',
            'password_repeat' => '确认密码',
            'position' => '职位',
            'office' => '所在办公室'
        ];
    }

    /**
     * 场景
     * @return array
     */
    public function scenarios()
    {
        //继承默认场景
        $scenarios = parent::scenarios();
        //新增登录场景
        $scenarios['change_avatar'] = ['avatar', 'sex'];
        //修改用户资料
        $scenarios[self::SCENARIO_SELF_UPDATE] = ['member_name', 'nickname', 'email', 'avatar', 'truename', 'identity',
            'sex', 'birthday', 'email_subscriptions', 'province_id', 'city_id', 'area_id', 'address', 'pca_info',
            'introduction','position','office'];
        return $scenarios;
    }

    /**
     * 加密密码
     * @param $password
     */
    public function setPassword($password)
    {
        $this->password = Yii::$app->security->generatePasswordHash($password);
    }

    /**
     * 生成登录token
     */
    public function generateToken()
    {
        $this->token = md5('m' . $this->member_id . Yii::$app->security->generateRandomString());
    }

    /**
     * 获取用户
     * @return Member
     */
    public function getMember()
    {
        if ($this->_Member === null) {
            $this->_Member = self::findByMobile($this->mobile);
        }
        return $this->_Member;
    }

    /**
     * 根据手机号码获取用户信息
     * @param $mobile
     * @return static
     */
    public static function findByMobile($mobile)
    {
        return self::findOne([
            'mobile' => $mobile
        ]);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getMemberAddresses()
    {
        return $this->hasMany(MemberAddress::className(), ['member_id' => 'member_id']);
    }

}
